package com.atguigu.tingshu.user.strategy;
import java.util.Date;
import com.google.common.collect.Maps;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;

@Component(value = "1002")
public class ProcessTrackOrderTypePayRecord implements ProcessDiffPayRecord{
    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;
    @Qualifier("com.atguigu.tingshu.album.client.AlbumInfoFeignClient")
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    /**
     * 处理声音订单类型支付记录
     * @param userPaidRecordVo 付费记录
     */
    @Override
    public void processPayRecord(UserPaidRecordVo userPaidRecordVo) {
        // user_paid_track表中插入数据
        String orderNo = userPaidRecordVo.getOrderNo();
        Long count = userPaidTrackMapper.selectCount(new LambdaQueryWrapper<UserPaidTrack>()
                .eq(UserPaidTrack::getOrderNo, orderNo));
        if (count != 0){
            throw new GuiguException(201, "该声音类型订单支付流水以存在");
        }
        List<Long> itemIdList = userPaidRecordVo.getItemIdList();
        Result<TrackInfo> trackInfoById = albumInfoFeignClient.getTrackInfoById(itemIdList.get(0));
        TrackInfo trackInfo = trackInfoById.getData();
        Assert.notNull(trackInfo, "远程调用获取声音信息失败");
        for (Long trackId : itemIdList) {
            UserPaidTrack userPaidTrack = new UserPaidTrack();
            userPaidTrack.setOrderNo(orderNo);
            userPaidTrack.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack.setAlbumId(trackInfo.getAlbumId());
            userPaidTrack.setTrackId(trackId);
            userPaidTrackMapper.insert(userPaidTrack);
        }

    }
}
